package cn.luckdou.business.demo1.controller;


import java.util.Date;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.luckdou.commons.base.BaseController;
import cn.luckdou.commons.result.PageInfo;
import cn.luckdou.business.demo1.model.Demo1;
import cn.luckdou.business.demo1.service.IDemo1Service;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

/**
 * demo1管理
 * @author Dadou
 * @DateTime 2018-06-01 
 **/
@Controller
@RequestMapping("/admin/demo1")
public class Demo1Controller extends BaseController {
	// log日志
	private static Logger logger = Logger.getLogger(Demo1Controller.class);
    
    @Autowired private IDemo1Service demo1Service;
    
    /**
	 * 列表页面
     */
	@RequiresPermissions("/admin/demo1/manager")
    @GetMapping("/manager")
    public String manager() {
        return "admin/business/demo1/demo1List";
    }

	/**
	 * 列表数据
     */
	@RequiresPermissions("/admin/demo1/manager")
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(Demo1 demo1, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<Demo1> wrapper = new EntityWrapper<Demo1>();
        wrapper.setEntity(demo1);
        Page<Demo1> pages = getPage(page, rows, sort, order);
        pages = demo1Service.selectPage(pages,wrapper);
        return pageToPageInfo(pages);
    }
    
    /**
	 * 添加页面
     */
	@RequiresPermissions("/admin/demo1/add")
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/business/demo1/demo1Add";
    }
    
    
    /**
	 * 添加数据
     */
	@RequiresPermissions("/admin/demo1/add")
    @PostMapping("/add")
    @ResponseBody
    public Object add(Demo1 demo1, String json) {
		demo1.setCreatedTime(new Date());
		demo1.setCreatedEditor(this.getShiroUser().getName());
        boolean b = demo1Service.insert(demo1);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 编辑页面
     */
	@RequiresPermissions("/admin/demo1/edit")
    @GetMapping("/editPage")
    public String editPage(Model model, String id) {
        Demo1 demo1 = demo1Service.selectById(id);
        model.addAttribute("demo1", demo1);
        return "admin/business/demo1/demo1Edit";
    }

    /**
     * 编辑数据
     */
	@RequiresPermissions("/admin/demo1/edit")
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Demo1 demo1, String json) {
		demo1.setUpdatedTime(new Date());
		demo1.setUpdatedEditor(this.getShiroUser().getName());
        boolean b = demo1Service.updateById(demo1);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

	/**
	 * 单个删除
	 */
	@RequiresPermissions("/admin/demo1/delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(String id) {
        boolean b = demo1Service.deleteById(Long.parseLong(id));
        if (b) {
			return renderSuccess("删除成功！");
        } else {
        	return renderError("删除失败！");
        }
	}

	/**
	 * 批量删除
	 */
	@RequiresPermissions("/admin/demo1/deleteIds")
	@PostMapping("/deleteIds")
	@ResponseBody
	public Object deleteIds(String ids) {
        if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
        	int num = 0;
        	for (String id : split) {
        		boolean b = demo1Service.deleteById(Long.parseLong(id));
        		if (b) num++;
        	}
        	if (num > 0) {
				return renderSuccess("删除"+num+"条记录成功！");
			} else {
				return renderError("删除失败！");
			}
		}
		return renderError("参数错误");
	}
    
}
